package com.cbl.v.ssczs.view.activity;

import android.widget.ListView;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.cbl.v.ssczs.R;
import com.cbl.v.ssczs.mode.entity.ItemBean;
import com.cbl.v.ssczs.mode.entity.LocalDataBean;
import com.cbl.v.ssczs.mode.net.HttpRequestForResponse;
import com.cbl.v.ssczs.mode.net.MyState;
import com.cbl.v.ssczs.mode.util.DialogUtils;
import com.cbl.v.ssczs.view.adapter.ItemAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;


public class DataListActivity extends BaseActivity {
    @BindView(R.id.freshListView)
    PullToRefreshListView refreshListView;
    private ListView lvData;
    private LocalDataBean dataBean;
    private ItemAdapter itemAdapter;
    private List<ItemBean> itemBeanList;
    @Override
    protected int getLayout() {
        return R.layout.activity_data_list;
    }

    @Override
    public void initView() {
        dataBean=new Gson().fromJson(getIntent().getStringExtra("data"),LocalDataBean.class);
        setTitle(dataBean.getTitle()+"开奖走势");
        itemBeanList=new ArrayList<>();
        itemAdapter=new ItemAdapter(getContext(),itemBeanList);
        itemAdapter.setLocalDataBean(dataBean);
        lvData=refreshListView.getRefreshableView();
        lvData.setAdapter(itemAdapter);
    }

    @Override
    public void initData() {
        onPullDownToRefresh(refreshListView);
    }

    @Override
    public void initListener() {
        refreshListView.setOnRefreshListener(this);
        refreshListView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        getData();
        super.onPullDownToRefresh(refreshView);
    }

    private void getData(){
        DialogUtils.showWithStatus(getContext());
        HttpRequestForResponse.getBallData(dataBean.getUrlAddress(),this, MyState.requestOne);
    }

    @Override
    public void dataBack(Object data, int order) {
        switch (order){
            case MyState.requestOne:
                List<ItemBean> list= (List<ItemBean>) data;
                itemBeanList.clear();
                itemBeanList.addAll(list);
                itemAdapter.notifyDataSetChanged();
                break;
        }
        super.dataBack(data, order);
    }

    @Override
    public void finishBack() {
        refreshListView.onRefreshComplete();
        DialogUtils.disMissDialog();
        super.finishBack();
    }
}
